﻿function resetColHeight(container) {
    var $family = $(container);
    var $cols = $family.find('.ag-step-col');
    $cols.css('min-height', 173);
    $cols.css('min-height', $family.height() - 3);
}

function SetDroppable(self, accepts, status, updatePanel) {
    $(".ag-step-col", $(self)).droppable({
        accept: accepts,
        activeClass: "issue-target-hover",
        drop: function (event, ui) {
            var $col = $(this);
            var id = ui.draggable.find('input[name="issue-id"]').val();
            // ajax request
            // $(updatePanel).replaceWith('hello world');
            var $container = ui.draggable.appendTo($col.find("div:first"));
            resetColHeight('#' + $col.parents('.ag-step-children').attr('id'));
        }
    });
}

function SetDraggable(container) {
    $(".ag-issue-wrapper", $(container)).draggable({
        revert: "invalid",
        helper: 'clone',
        scroll: false,
        containment: container,
        cancel: "div.ag-subhead",
        cursor: "move"
    });
}

function InitDroppable(aodroppable) {
    while (aodroppable.length != 0) {
        var drop = aodroppable.pop();
        SetDroppable(drop.self, drop.accepts, drop.status, drop.updatePanel);
    }
}

function InitDraggable(aodraggable) {
    while (aodraggable.length != 0) {
        var drag = aodraggable.pop();
        resetColHeight(drag.container);     // Initial Column height
        SetDraggable(drag.container);
    }
}

function resetCardWidth() {
    var cardWidth = $(".ag-step-col").width() / 2 - 18;
    var cardStyle = document.getElementById("js-cardwidth");
    // fix a bug on IE8
    if ($.browser.msie)
        cardStyle.styleSheet.cssText = "#ag.ag-board-task .ag-cards .ag-issue{width:" + cardWidth + "px;}";
    else
        cardStyle.innerHTML = "#ag.ag-board-task .ag-cards .ag-issue{width:" + cardWidth + "px;}";
}